#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

class RangeList {

public:
    void add(int l, int r) {
        auto it = s.lower_bound({ l, 0 });
        while (it != s.end() && r >= it->second) {
            l = min(l, it->second);
            r = max(r, it->first);
            it = s.erase(it);
        }
        s.insert({r, l});
    }

    void remove(int l, int r) {
        add(l, r);
        auto it = s.lower_bound({ l, 0 });
        if (it == s.end()) return;
        int nl1 = it->second, nr1 = l;
        if (nl1 < nr1) s.insert({ nr1, nl1 });
        int nl2 = r, nr2 = it->first;
        if (nl2 < nr2) s.insert({ nr2, nl2 });
        s.erase(it);
    }
    void print() {
        for (auto it : s) {
            printf("[%d,%d) ", it.second, it.first);
        }
        printf("\n");
        cout << s.size() << endl;
    }
private:
    set<pii> s;
};
int main() {
    RangeList rl;
    rl.add(1, 5);
    rl.print();
    rl.add(10, 20);
    rl.print();
    rl.add(20, 20);
    rl.print();
    rl.add(20, 21);
    rl.print();
    rl.add(2, 4);
    rl.print();
    rl.add(3, 8);
    rl.print();
    rl.remove(10, 10);
    rl.print();
    rl.remove(10, 11);
    rl.print();
    rl.remove(15, 17);
    rl.print();
    rl.remove(3, 19);
    rl.print();

}
